package com.itheima.controller;

import com.itheima.domain.UmsAdmin;
import com.itheima.domain.UmsAdminRoleRelation;
import com.itheima.domain.UmsRole;
import com.itheima.manager.UmsAdminManager;
import com.itheima.util.R;
import com.itheima.vo.PageBeanVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/admin")
public class UmsAdminController {

    @Autowired
    UmsAdminManager umsAdminManager;

    //登陆
    @PostMapping("/login")
    public R login(@RequestBody Map<String ,String> map){
        return umsAdminManager.login(map);
    }

    //获取当前登录用户信息
    @GetMapping("/info")
    public R getAdminInfo(){
        return umsAdminManager.getAdminInfo();
    }

    //登出
    @PostMapping("/logout")
    public R logout(@RequestHeader("Authorization")String token){
        return umsAdminManager.logout(token);
    }

    //刷新token
    @GetMapping("/refreshToken")
    public R refreshToken(@RequestHeader("Authorization")String token){
        return umsAdminManager.refreshToken(token);
    }

    //用户列表
    @GetMapping("/list")
    public R list(@RequestParam Integer pageNum,@RequestParam Integer pageSize,@RequestParam(value="keyword",required=false) String keyword){
        PageBeanVo list = umsAdminManager.list(pageNum, pageSize, keyword);
        return R.success(list);
    }

    //添加用户
    @PostMapping("/register")
    public R register(@RequestBody UmsAdmin umsAdmin){
        umsAdminManager.register(umsAdmin);
        return R.success("");
    }

    @PostMapping("/update/{id}")
    public R update(@RequestBody UmsAdmin umsAdmin,@PathVariable("id") Long id){
        umsAdminManager.update(umsAdmin,id);
        return R.success("");
    }

    @PostMapping("/delete/{id}")
    public R delete(@PathVariable long id){
        umsAdminManager.delete(id);
        return R.success("");
    }

    //查询用户角色
    @GetMapping("/role/{adminId}")
    public R role(@PathVariable long adminId){
        List<UmsAdminRoleRelation> list= umsAdminManager.role(adminId);
        return R.success(list);
    }

    @PostMapping("/role/update")
    public R roleUpdate(@RequestBody HashMap<String,Object> map){
        umsAdminManager.roleUpdate(map);
        return R.success("");
    }

    //修改账号状态
    @PostMapping("/updateStatus/{id}")
    public R updateStatus(@PathVariable Long id, @RequestParam("status") Integer status){
        umsAdminManager.updateStatus(id,status);
        return R.success("");
    }

    //查询角色列表
//    @GetMapping("role/listAll")
//    public R roleList(){
//
//    }
}
